package com.xyzwps.orm.mybatis;

import com.xyzwps.orm.mybatis.mapper.TagMapper;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

public class App {

    private final SqlSessionFactory sqlSessionFactory;

    public App() {
        var ds = new HikariDataSource();
        ds.setJdbcUrl("jdbc:mysql://localhost:3306/orm");
        ds.setDriverClassName("com.mysql.cj.jdbc.Driver");
        ds.setUsername("scott");
        ds.setPassword("tiger");
        var environment = new Environment("xx", new JdbcTransactionFactory(), ds);
        var config = new Configuration(environment);
        config.setMapUnderscoreToCamelCase(true);
        config.addMapper(TagMapper.class);
        this.sqlSessionFactory = new SqlSessionFactoryBuilder().build(config);
    }

    public SqlSessionFactory getSqlSessionFactory() {
        return sqlSessionFactory;
    }

    public TagMapper getTagMapper() {
        return sqlSessionFactory.openSession(true).getMapper(TagMapper.class);
    }
}
